var a = getApp(), t = null, e = null, d = "", i = {};

Page({
    data: {
        loadingImg: !0,
        card_id: "",
        cardData: {
            card_statistics: {}
        },
        cardDataInfo: {
            card_statistics: {}
        },
        area_name: "",
        role_name: {},
        drawing: [],
        savebtnText: "保存名片图片",
        avatar: "",
        card_mobile: "/image/card_img_b.png",
        card_wechat: "/image/card_img_c.png",
        area_nameIcon: "/image/card_img_d.png",
        visit: "/image/card_img_a.png",
        canvasWidth: "",
        canvasHeight: "",
        showBtn: !1,
        ithumb: ""
    },
    onLoad: function(i) {
        var n = this;
        this.setData({
            card_id: i.card_id
        }), i.mid && a._setMid(i.mid), wx.getSystemInfo({
            success: function(a) {
                d = a.windowWidth / 375, t = 343 * d, e = 510 * d, n.setData({
                    canvasWidth: t,
                    canvasHeight: e
                });
            }
        }), wx.showLoading({
            title: "绘图中"
        }), "share" === i.from ? (this.setData({
            from_sta: 1
        }), this.getCardDate()) : this.startDraw();
    },
    onReady: function() {},
    onShow: function() {
        wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        var a = wx.getStorageSync("yz_uid"), t = "";
        return a && (t = a), {
            query: "card_id=".concat(this.data.card_id, "&mid=").concat(t, "&from=share")
        };
    },
    onShareAppMessage: function() {
        var a = wx.getStorageSync("yz_uid"), t = "";
        return a && (t = a), {
            path: "/packageB/member/business_card/BusinessCard/BusinessCard?card_id=" + this.data.card_id + "&mid=" + t + "&from=share"
        };
    },
    startDraw: function() {
        var a = this;
        wx.getStorage({
            key: "iconFrom",
            success: function(t) {
                a.setData({
                    visit: t.data.visit,
                    card_mobile: t.data.card_mobile,
                    card_wechat: t.data.card_wechat,
                    area_nameIcon: t.data.area_nameIcon,
                    "cardData.card_avatar": t.data.card_avatar,
                    "cardData.background": t.data.background,
                    "cardData.min_code": t.data.min_code
                }), wx.getStorage({
                    key: "info",
                    success: function(t) {
                        a.setData({
                            "cardData.card_name": t.data.card_name,
                            "cardData.card_mobile": t.data.card_mobile,
                            "cardData.card_wechat": t.data.card_wechat,
                            "cardData.area_name": t.data.area_name,
                            "cardData.card_statistics.visit": t.data.visit,
                            "cardData.shop_name": t.data.shop_name,
                            "cardData.role_name": t.data.role_name
                        }), a.getAvaterInfo();
                    },
                    fail: function(a) {
                        wx.showToast({
                            title: a,
                            icon: "none"
                        });
                    }
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: a,
                    icon: "none"
                });
            }
        });
    },
    getCardDate: function() {
        var t = this, e = a.getNetAddresss("plugin.business-card.frontend.controllers.center.card.index");
        a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var d = null;
                    0 != e.data.area_name.length && (null == e.data.area_name[3] && (e.data.area_name[3] = ""), 
                    null == e.data.has_one_card.address && (e.data.has_one_card.address = ""), d = e.data.area_name[0] + e.data.area_name[1] + e.data.area_name[2] + e.data.area_name[3] + e.data.has_one_card.address), 
                    t.setData({
                        cardData: e.data,
                        cardMessage: e.data.has_one_card,
                        role_name: e.data.role_name,
                        area_name: d
                    }), t.data.from_sta || t.setData({
                        card_id: e.data.has_one_card.id
                    }), t.getData();
                } else wx.showToast({
                    title: e.msg,
                    icon: "none"
                }), setTimeout(function() {
                    wx.reLaunch({
                        url: "/packageG/index/index"
                    });
                }, 2e3);
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getData: function() {
        var t = this, e = a.getNetAddresss("plugin.business-card.frontend.controllers.center.card-pic.index");
        e += "&card_id=" + this.data.card_id + "&card_type=1", a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    t.setData({
                        cardDataInfo: e.data,
                        role_name: e.data.role_name
                    }), t.saveImg();
                    var d = "";
                    t.data.cardData.area_name[0] && (d = t.data.cardData.area_name[0] + t.data.cardData.area_name[1] + t.data.cardData.area_name[2] + t.data.cardData.area_name[3] + t.data.cardData.has_one_card.address);
                    var i = {};
                    null == e.data.role_name.level_name && (e.data.role_name.level_name = ""), i.card_name = e.data.card_name, 
                    i.card_mobile = e.data.card_mobile, i.card_wechat = e.data.card_wechat, i.area_name = d, 
                    i.visit = "人气" + e.data.card_statistics.visit, i.card_name = e.data.card_name + "名片码", 
                    i.shop_name = "名片由【".concat(e.data.shop_name, "】生成"), i.role_name = e.data.role_name.role_name + "|" + e.data.role_name.level_name, 
                    wx.setStorage({
                        key: "info",
                        data: i
                    });
                }
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    saveImg: function() {
        var a = this;
        wx.getImageInfo({
            src: this.data.visit,
            success: function(t) {
                console.log(t), a.setData({
                    visit: t.path
                }), i.visit = a.data.visit, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {
                console.log(a, "错误的回调1");
            }
        }), wx.getImageInfo({
            src: this.data.card_mobile,
            success: function(t) {
                console.log(t), a.setData({
                    card_mobile: t.path
                }), i.card_mobile = a.data.card_mobile, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {
                console.log(a, "错误的回调2");
            }
        }), wx.getImageInfo({
            src: this.data.card_wechat,
            success: function(t) {
                a.setData({
                    card_wechat: t.path
                }), i.card_wechat = a.data.card_wechat, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {
                console.log(a, "错误的回调3");
            }
        }), wx.getImageInfo({
            src: this.data.area_nameIcon,
            success: function(t) {
                a.setData({
                    area_nameIcon: t.path
                }), i.area_nameIcon = a.data.area_nameIcon, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {
                console.log(a, "错误的回调4");
            }
        }), wx.downloadFile({
            url: this.data.cardDataInfo.background,
            success: function(t) {
                var e = t.tempFilePath;
                a.setData({
                    "cardDataInfo.background": e
                }), i.background = t.tempFilePath, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {}
        }), wx.downloadFile({
            url: this.data.cardDataInfo.card_avatar,
            success: function(t) {
                a.setData({
                    "cardDataInfo.card_avatar": t.tempFilePath
                }), i.card_avatar = t.tempFilePath, wx.setStorage({
                    key: "iconFrom",
                    data: i
                }), setTimeout(function() {
                    a.startDraw();
                }, 2e3);
            },
            fail: function(a) {
                console.log(a, "失败");
            }
        }), wx.downloadFile({
            url: this.data.cardDataInfo.min_code,
            success: function(t) {
                a.setData({
                    "cardDataInfo.min_code": t.tempFilePath
                }), i.min_code = t.tempFilePath, wx.setStorage({
                    key: "iconFrom",
                    data: i
                });
            },
            fail: function(a) {
                console.log(a, "错误的回调");
            }
        });
    },
    getAvaterInfo: function() {
        var t = wx.createCanvasContext("myCanvas");
        t.fillStyle = "#FFFFFF", t.fillRect(0, 0, this.data.canvasWidth + 20, this.data.canvasHeight), 
        a._isTextEmpty(this.data.cardData.background) || t.drawImage(this.data.cardData.background, (this.data.canvasWidth + 20 - 335 * d) / 2, 20, 335 * d, 160 * d);
        var e = 15 * d, i = 181 * d;
        t.save(), t.beginPath(), t.arc(30 + e, 30 + i + 20, 30, 0, 2 * Math.PI, !1), t.clip(), 
        a._isTextEmpty(this.data.cardData.card_avatar) || t.drawImage(this.data.cardData.card_avatar, e, i + 20, 60, 60), 
        t.restore(), t.setTextAlign("left"), t.setFillStyle("#000000"), t.setFontSize(14), 
        console.log(this.data.cardData.card_name, "this.data.cardData.card_name"), this.data.cardData.card_name.length > 15 ? (this.data.cardData.card_name1 = this.data.cardData.card_name.substr(0, 15), 
        this.data.cardData.card_name2 = this.data.cardData.card_name.substr(15), t.fillText(this.data.cardData.card_name1, 90 * d, 210 * d)) : t.fillText(this.data.cardData.card_name, 90 * d, 230 * d), 
        this.data.cardData.card_name2 && t.fillText(this.data.cardData.card_name2, 90 * d, 230 * d), 
        t.setTextAlign("left"), t.setFillStyle("#8c8c8c"), t.setFontSize(14), t.fillText(this.data.cardData.role_name, 90 * d, 250 * d), 
        "" != this.data.cardData.card_mobile && (t.drawImage(this.data.card_mobile, 15 * d, 275 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.card_mobile, 40 * d, 290 * d)), 
        "" != this.data.cardData.card_wechat && ("" == this.data.cardData.card_mobile ? (t.drawImage(this.data.card_wechat, 15 * d, 275 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.card_wechat, 40 * d, 290 * d)) : (t.drawImage(this.data.card_wechat, 15 * d, 300 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.card_wechat, 40 * d, 315 * d))), 
        "" != this.data.cardData.area_name && (this.data.cardData.area_name.length > 15 ? (this.data.cardData.area_name1 = this.data.cardData.area_name.substr(0, 15), 
        this.data.cardData.area_name2 = this.data.cardData.area_name.substr(15)) : this.data.cardData.area_name1 = this.data.cardData.area_name, 
        "" == this.data.cardData.card_mobile && "" == this.data.cardData.card_wechat ? (t.drawImage(this.data.area_nameIcon, 15 * d, 270 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.area_name1, 40 * d, 285 * d), 
        this.data.cardData.area_name2 && t.fillText(this.data.cardData.area_name2, 40 * d, 300 * d)) : "" == this.data.cardData.card_mobile || "" == this.data.cardData.card_wechat ? (t.drawImage(this.data.area_nameIcon, 15 * d, 300 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.area_name1, 40 * d, 315 * d), 
        this.data.cardData.area_name2 && t.fillText(this.data.cardData.area_name2, 40 * d, 330 * d)) : (t.drawImage(this.data.area_nameIcon, 15 * d, 325 * d, 20 * d, 20 * d), 
        t.setTextAlign("left"), t.setFillStyle("#b0b0b0"), t.setFontSize(12), t.fillText(this.data.cardData.area_name1, 40 * d, 340 * d), 
        this.data.cardData.area_name2 && t.fillText(this.data.cardData.area_name2, 40 * d, 355 * d))), 
        t.drawImage(this.data.visit, 280 * d, 205 * d, 20 * d, 35 * d), t.setTextAlign("center"), 
        t.setFillStyle("#f15353"), t.setFontSize(14), t.fillText(this.data.cardData.card_statistics.visit, 315 * d, 230 * d), 
        t.drawImage(this.data.cardData.min_code, 25 * d, 385 * d, 100, 100), t.setTextAlign("center"), 
        t.setFillStyle("#50a6f9"), t.setFontSize(14), this.data.cardData.card_name.length > 15 ? (this.data.cardData.card_name1 = this.data.cardData.card_name.substr(0, 15), 
        this.data.cardData.card_name2 = this.data.cardData.card_name.substr(15), t.fillText(this.data.cardData.card_name1, 230 * d, 406 * d)) : t.fillText(this.data.cardData.card_name, 230 * d, 416 * d), 
        this.data.cardData.card_name2 && t.fillText(this.data.cardData.card_name2, 230 * d, 420 * d), 
        t.setTextAlign("center"), t.setFillStyle("#333"), t.setFontSize(14), t.fillText("长按识别名片码保存名片", 240 * d, 437 * d), 
        t.setTextAlign("center"), t.setFillStyle("#8c8c8c"), t.setFontSize(13), t.fillText(this.data.cardData.shop_name, 240 * d, 460 * d), 
        t.draw(!0, this.toimage());
    },
    toimage: function() {
        var a = this;
        wx.hideLoading(), this.setData({
            showBtn: !0
        });
        var t = this;
        setTimeout(function() {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: a.data.canvasWidth + 20,
                height: 4 * a.data.canvasHeight,
                destWidth: 4 * a.data.canvasWidth + 80,
                destHeight: a.data.canvasHeight,
                canvasId: "myCanvas",
                success: function(a) {
                    console.log(a);
                    var e = a.tempFilePath;
                    t.setData({
                        ithumb: e
                    });
                },
                fail: function(a) {
                    console.log(a);
                }
            });
        }, 1e3);
    },
    previewImage: function() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: this.data.canvasWidth + 20,
            height: this.data.canvasHeight,
            destWidth: 4 * this.data.canvasWidth + 80,
            destHeight: 4 * this.data.canvasHeight,
            canvasId: "myCanvas",
            success: function(a) {
                var t = a.tempFilePath;
                console.log(t), wx.showToast({
                    title: "长按分享好友！",
                    icon: "none",
                    duration: 2e3
                }), wx.previewImage({
                    current: t,
                    urls: [ t ]
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    saveShareImg: function() {
        wx.showLoading({
            title: "保存中"
        }), wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: this.data.canvasWidth + 20,
            height: this.data.canvasHeight,
            destWidth: 4 * this.data.canvasWidth + 80,
            destHeight: 4 * this.data.canvasHeight,
            canvasId: "myCanvas",
            success: function(a) {
                console.log(a), wx.saveImageToPhotosAlbum({
                    filePath: a.tempFilePath,
                    success: function(a) {
                        wx.showToast({
                            title: "图片保存成功",
                            icon: "success",
                            duration: 2e3
                        });
                    },
                    fail: function(a) {
                        wx.hideLoading();
                    }
                });
            }
        });
    }
});